package com.joseph.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.joseph.annotation.CheckRepeat;
import com.joseph.annotation.TokenToView;
import com.joseph.base.BaseController;
import com.joseph.model.User;
import com.joseph.service.UserService;

/**
 * @description 
 * @author Joseph_Mok
 * @date 2016年1月20日下午3:01:39
 */

@Controller
public class LoginController extends BaseController {

	@Resource
	private UserService userService;
	
	@TokenToView
	@RequestMapping(value="/")
	public String loginView(HttpServletRequest request) {
		return "login";
	}
	
	@TokenToView
	@RequestMapping(value="/index")
	public String indexView(HttpServletRequest request) {
		return "index";
	}
	
	@CheckRepeat
	@RequestMapping(value="/login")
	public String login(@ModelAttribute User user) {
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
		//token.setRememberMe(true);
		Subject currentUser = SecurityUtils.getSubject();
		try{
			currentUser.login(token);  
		}catch (UnknownAccountException e) {
			System.out.println("未知用户名!");
		}catch (AuthenticationException e) {
			System.out.println("密码错误!");
		}catch (Exception e) {
			System.out.println("服务器错误!");
		}
		return "redirect:/index";
	}   
}
